package com.woniuxy.jiapei.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.jiapei.exception.MyException;
import com.woniuxy.jiapei.pojo.Hospital;
import com.woniuxy.jiapei.pojo.User;
import com.woniuxy.jiapei.service.HospitalService;
import com.woniuxy.jiapei.service.UserService;
import com.woniuxy.jiapei.util.LvFeiLongDateUtils;
import com.woniuxy.jiapei.util.R;
import com.woniuxy.jiapei.util.ResultCode;
import com.woniuxy.jiapei.util.ResultMsg;
import com.woniuxy.jiapei.vo.HospitalQuery;
import com.woniuxy.jiapei.vo.HospitalVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lr
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/jiapei/hospital")
@CrossOrigin
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private UserService userService;

    @GetMapping("/hospitals")
    @ApiOperation("分页查询所有医院")
    public R getHospitalList(@ApiParam("当前页") @RequestParam(value = "page", required = true, defaultValue = "1") Integer page, @ApiParam("分页标准") @RequestParam(value = "limit", required = true, defaultValue = "2") Integer limit, HospitalQuery hospitalQuery) throws MyException, ParseException {

        Date[] dateList = LvFeiLongDateUtils.parseDateStrType(hospitalQuery.getStartTime(), hospitalQuery.getEndTime());

        Page pageBean = new Page(page, limit);
        QueryWrapper queryWrapper = new QueryWrapper();

        if (!StringUtils.isEmpty(hospitalQuery.getHospitalName())) {
            queryWrapper.like("name", hospitalQuery.getHospitalName());
        }
        if (!StringUtils.isEmpty(hospitalQuery.getProvince())) {
            queryWrapper.like("province", hospitalQuery.getProvince());
        }
        if (!StringUtils.isEmpty(hospitalQuery.getCity())) {
            queryWrapper.eq("city", hospitalQuery.getCity());
        }

        if (!StringUtils.isEmpty(hospitalQuery.getDistrict())) {
            queryWrapper.eq("district", hospitalQuery.getDistrict());
        }

        if (!StringUtils.isEmpty(hospitalQuery.getStartTime())) {
            queryWrapper.ge("time", dateList[0]);
        }
        if (!StringUtils.isEmpty(hospitalQuery.getEndTime())) {
            queryWrapper.le("time", dateList[1]);
        }

        if (!StringUtils.isEmpty(hospitalQuery.getGainshare())) {
            switch (hospitalQuery.getGainshare()) {
                case 1:
                    queryWrapper.ge("income", 0);
                    queryWrapper.lt("income", 10);
                    break;
                case 2:
                    queryWrapper.ge("income", 10);
                    queryWrapper.lt("income", 20);
                    break;
                case 3:
                    queryWrapper.ge("income", 20);
                    queryWrapper.lt("income", 30);
                    break;
                case 4:
                    queryWrapper.ge("income", 30);
                    queryWrapper.lt("income", 50);
                    break;
                case 5:
                    queryWrapper.ge("income", 50);
                    queryWrapper.lt("income", 100);
                    break;
            }
        }

        Page resultPage = hospitalService.page(pageBean, queryWrapper);
        List<Hospital> records = resultPage.getRecords();
        long total = resultPage.getTotal();

        List<HospitalVo> hospitalVos = new ArrayList<>();

        if (records != null && records.size() > 0) {
            for (Hospital hospital : records) {
                User user = userService.getById(hospital.getLinkman());
                HospitalVo hospitalVo = new HospitalVo();
                hospitalVo.setHospital(hospital);
                hospitalVo.setUser(user);
                hospitalVos.add(hospitalVo);
            }
        }


        if (records != null && records.size() > 0) {
            return R.ok().data("records", hospitalVos).data("total", total);
        } else {
            throw new MyException(ResultCode.GETHOSPITALBYPAGEFAILED, ResultMsg.GETHOSPITALBYPAGEFAILEDMSG);
        }

    }

    @PostMapping("/hospitals")
    @ApiOperation("新增医院")
    public R addHospital(@RequestBody Hospital hospital) throws MyException {
        boolean save = hospitalService.save(hospital);
        if (save) {
            return R.ok().message("新增医院信息成功");
        } else {
            throw new MyException(ResultCode.ADDHOSPITALFAILED, ResultMsg.ADDHOSPITALFAILEDMSG);
        }
    }

    @GetMapping("/hospitalDetail/{id}")
    @ApiOperation("获取医院详情信息")
    public R getHospitalDetail(@PathVariable Integer id) throws MyException {
        Hospital hospital = hospitalService.getById(id);
        if (hospital != null) {
            return R.ok().data("hospital", hospital);
        } else {
            throw new MyException(ResultCode.GETHOSPITALDETAILFAILED, ResultMsg.GETHOSPITALDETAILFAILEDMSG);
        }
    }

    @DeleteMapping("/hospital/{id}")
    @ApiOperation("刪除医院信息")
    public R deleteHospital(@PathVariable Integer id) throws MyException {
        boolean res = hospitalService.removeById(id);
        if (res) {
            return R.ok().message("删除医院信息成功");
        } else {
            throw new MyException(ResultCode.DELETEHOSPITALFAILED, ResultMsg.DELETEHOSPITALFAILEDMSG);
        }
    }

    @PutMapping("/hospitals")
    @ApiOperation("修改医院信息")
    public R updateHospital(@RequestBody Hospital hospital) throws MyException {
        boolean res = hospitalService.updateById(hospital);
        if (res) {
            return R.ok().message("修改医院信息成功");
        } else {
            throw new MyException(ResultCode.UPDATEHOSPITALFAILED, ResultMsg.UPDATEHOSPITALFAILEDMSG);
        }
    }
}

